package com.littlebee.storychild.common.utils.sharePrefUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 * 描述：
 */
public class SharePrefUtil extends BaseSharePref {
    private String kSharePreferenceUser = "tuliuShare";
    SharedPreferences sp = null;
    SharedPreferences.Editor editor;
    private static SharePrefUtil userStatus;

    public SharePrefUtil(Context context) {
        sp = context.getSharedPreferences(kSharePreferenceUser, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    public static SharePrefUtil getInstance(Context context) {
        if (userStatus == null) {
            userStatus = new SharePrefUtil(context);
        }
        return userStatus;
    }

    public void clearData() {
        editor.clear().commit();
    }

    @Override
    public void setObject(String key, Object value) {
        if (editor!=null) {
            if (value instanceof String) {
                editor.putString(key, (String)value);
            } else if (value instanceof Boolean) {
                editor.putBoolean(key, (Boolean)value);
            } else if (value instanceof Integer) {
                editor.putInt(key, (Integer)value);
            } else {
                try {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(baos);
                    oos.writeObject(value);
                    String personBase64 = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
                    editor.putString(key, personBase64);
                    editor.commit();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            editor.commit();
        }
    }


    public Object getObject(String key) {
        try {
            String personBase64 = sp.getString(key, "");
            byte[] base64Bytes = Base64.decode(personBase64.getBytes(), Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            Object object = ois.readObject();
            return object;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public String getStringValue(String key) {
        // TODO Auto-generated method stub
        if (sp!=null)
            return sp.getString(key, "");
        return "";
    }

    public String getStringValue(String key, String defaultValue) {
        // TODO Auto-generated method stub
        if (sp!=null)
            return sp.getString(key, defaultValue);
        return defaultValue;
    }

    @Override
    public boolean getBoolValue(String key) {
        // TODO Auto-generated method stub
        if (sp!=null)
            return sp.getBoolean(key, false);
        return false;
    }

    public boolean getBoolValue(String key, boolean defaultValue) {
        // TODO Auto-generated method stub
        if (sp!=null)
            return sp.getBoolean(key, defaultValue);
        return defaultValue;
    }

    @Override
    public int getIntValue(String key) {
        if (sp!=null)
            return sp.getInt(key, 0);
        return 0;
    }

    public int getIntValue(String key, int defaultValue) {
        if (sp!=null)
            return sp.getInt(key, defaultValue);
        return defaultValue;
    }

    public void setObject(String key, ArrayList<String> arrayString) {
        String regularEx = "#";
        String str = "";
        if (arrayString != null && arrayString.size() > 0) {
            for (String value : arrayString) {
                str += value;
                str += regularEx;
            }
            editor.putString(key, str);
            editor.commit();
        } else {
            editor.putString(key, "");
            editor.commit();
        }
    }

    public ArrayList<String> getArrayValue(String key) {
        String regularEx = "#";
        String[] str = null;
        String values = sp.getString(key, "");
        ArrayList<String> arrayList = new ArrayList<String>();
        if (values!=null && !values.equals("")) {
            str = values.split(regularEx);
            //        Collections.addAll(arrayList, str);
            for (int i = str.length; i > 0; i --)
                arrayList.add(str[i - 1]);
        }
        return arrayList;
    }

    public static String kUserFaceUrl = "avatar";
    public static String kUserIsFrozze = "userisfrozze";
    public static String kUserInfo = "userInfo";
    public static String kPriceInfo = "priceInfo";

    public static String kAccountInfo = "accountInfo";
    public static String kToken = "token";
    public static String kIgoreVersion = "igoreVersion";
}
